/**
 * 
 */
package com.worksmap.core.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.taobao.api.domain.Order;
import com.worksmap.common.GeneralDao;
import com.worksmap.core.constant.RateConstant;
import com.worksmap.core.dao.TradeDao;
import com.worksmap.core.entity.SellerConfig;
import com.worksmap.core.entity.TaoBaoSeller;
import com.worksmap.core.entity.Trade;
import com.worksmap.core.util.ObjectUtil;

/**
 * @author jack
 *
 */
@Service
public class TradeDaoImpl extends GeneralDao implements TradeDao {

	@Override
	public boolean saveTrade(Trade trade) {
		try {
			return this.saveEntity("INSERT INTO trade ( " + ObjectUtil.getClassFieldStr(Trade.class).replace(",id,", "") + ")" +
					"values(:tid,:oid,:seller_nick,:buyer_nick,:rate_status_buyer,:rate_by_us,:local_create_time)", 
					trade);
		} catch (Exception e) {
			logger.error("保存订单错误！" + e);
		}
		return false;
	}

	@Override
	public boolean updateTrade(Trade trade, String... fields) {
		if(trade == null){
			return false;
		}
		try {
			return this.updateEntity(trade, fields);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("更新订单trade错误！" + trade + e);
		}
		return false;
	}

	@Override
	public Order getOrderDetail(String oId) {
		
		return null;
	}

	@Override
	public List<Trade> getTradeBySellerConfig(TaoBaoSeller seller,
			SellerConfig sellerConfig) {
		List<Trade> tradeList = null;
		List<Object> valueList = new ArrayList<Object>();
		StringBuilder sqlStr = new StringBuilder(" select * from trade where 1 = 1 ");
		if(RateConstant.BUYER_RATE_ALREADY.equals(sellerConfig.getTrade_rate_condition())){
			sqlStr.append(" and rate_status_buyer = ? ");
			valueList.add(RateConstant.BUYER_RATE_ALREADY);
		}
		try {
			tradeList = this.getEntityList(Trade.class, sqlStr.toString() , valueList.toArray());
		} catch (Exception e) {
			logger.error("查询用户列表错误！" + e);
		}
		return tradeList;
	}

}
